﻿using Microsoft.Practices.ServiceLocation;
using MyToolkit.Multimedia;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using YoutubeApp.Model;
using YoutubeApp.Services;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace YoutubeApp.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class HomePage : Page
    {
        public HomePage()
        {
            this.InitializeComponent();
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            Frame frame = Window.Current.Content as Frame;
            if (frame == null)
            {
                return;
            }

            if (frame.CanGoBack)
            {
                frame.GoBack();
                e.Handled = true;
            }
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private IHomeViewModel homeViewModel
        {
            get { return (IHomeViewModel)DataContext; }
        }
        private async void Videos_ItemClick(object sender, ItemClickEventArgs e)
        {
            homeViewModel.LoadingComplete = true;
            var getDat = (YoutubeVideo)e.ClickedItem;
            YouTubeUri getVideos = await homeViewModel.GetVideoUriAsync(getDat.Id, MyToolkit.Multimedia.YouTubeQuality.Quality720P);
            homeViewModel.LoadingComplete = false;
            if (getVideos != null && getVideos.Uri != null)
            {
                Frame fm = Window.Current.Content as Frame;
                fm.Navigate(typeof(MoviesPage), new PlayerItem() 
                { 
                    image = getDat.Thumbnail.ToString(),
                    link = getVideos.Uri, 
                    name = getDat.Title,
                    VideoQuality = getVideos.VideoQuality 
                });
            }
            else
            {
                new MessageDialog("Get Movie Fail").ShowAsync();
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (orderCbx != null)
            {
                string content = ((ComboBoxItem)orderCbx.SelectedItem).Tag.ToString();

                homeViewModel.GetYoutubeVideo("50", content);
            }
        }
    }
}
